home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / alloc.h < prev    next >
Text File  |  1995-12-04  |  9KB  |  236 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * Resource allocation routines...
  57.  *
  58.  * designed so that we don't have to keep track of EVERYTHING so that
  59.  * it can be explicitly freed later (a fundamentally unsound strategy ---
  60.  * particularly in the presence of die()).
  61.  *
  62.  * Instead, we maintain pools, and allocate items (both memory and I/O
  63.  * handlers) from the pools --- currently there are two, one for per
  64.  * transaction info, and one for config info.  When a transaction is over,
  65.  * we can delete everything in the per-transaction pool without fear, and
  66.  * without thinking too hard about it either.
  67.  *
  68.  * rst
  69.  */
  70.  
  71. /* Arenas for configuration info and transaction info
  72.  * --- actual layout of the pool structure is private to 
  73.  * alloc.c.  
  74.  */
  75.  
  76. typedef struct pool pool;
  77.  
  78. extern pool *permanent_pool;
  79. void init_alloc();        /* Set up everything */
  80. pool *make_sub_pool (pool *);    /* All pools are subpools of permanent_pool */
  81. void destroy_pool (pool *);
  82.  
  83. /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
  84.  
  85. void clear_pool (struct pool *);
  86.  
  87. /* Preparing for exec() --- close files, etc., but *don't* flush I/O
  88.  * buffers, *don't* wait for subprocesses, and *don't* free any memory.
  89.  */
  90.  
  91. void cleanup_for_exec ();
  92.  
  93. /* routines to allocate memory from an pool... */
  94.  
  95. void *palloc(struct pool *, int nbytes);
  96. void *pcalloc(struct pool *, int nbytes);
  97. char *pstrdup(struct pool *, char *s);
  98. char *pstrcat(struct pool *, ...); /* all '...' must be char* */
  99.  
  100. /* array and alist management... keeping lists of things.
  101.  * Common enough to want common support code ...
  102.  */
  103.  
  104. typedef struct {
  105.     pool *pool;
  106.     int elt_size;
  107.     int nelts;
  108.     int nalloc;
  109.     char *elts;
  110. } array_header;
  111.  
  112. array_header *make_array (pool *p, int nelts, int elt_size);
  113. void *push_array (array_header *);
  114. void array_cat (array_header *dst, array_header *src);
  115. array_header *append_arrays (pool *, array_header *, array_header *);
  116.  
  117. /* copy_array copies the *entire* array.  copy_array_hdr just copies
  118.  * the header, and arranges for the elements to be copied if (and only
  119.  * if) the code subsequently does a push or arraycat.
  120.  */
  121.      
  122. array_header *copy_array (pool *p, array_header *src);
  123. array_header *copy_array_hdr (pool *p, array_header *src);
  124.                
  125.  
  126. /* Tables.  Implemented alist style, for now, though we try to keep
  127.  * it so that imposing a hash table structure on top in the future
  128.  * wouldn't be *too* hard...
  129.  *
  130.  * Note that key comparisons for these are case-insensitive, largely
  131.  * because that's what's appropriate and convenient everywhere they're
  132.  * currently being used...
  133.  */
  134.  
  135. typedef array_header table;     
  136.      
  137. typedef struct {
  138.     char *key;            /* maybe NULL in future;
  139.                  * check when iterating thru table_elts
  140.                  */
  141.     char *val;
  142. } table_entry;
  143.  
  144. table *make_table (pool *p, int nelts);
  145. table *copy_table (pool *p, table *);     
  146. char *table_get (table *, char *);
  147. void table_set (table *, char *name, char *val);
  148. void table_merge (table *, char *name, char *more_val);
  149.  
  150. table *overlay_tables (pool *p, table *overlay, table *base);     
  151.  
  152. array_header *table_elts (table *);     
  153.  
  154. /* routines to remember allocation of other sorts of things...
  155.  * generic interface first.  Note that we want to have two separate
  156.  * cleanup functions in the general case, one for exec() preparation,
  157.  * to keep CGI scripts and the like from inheriting access to things
  158.  * they shouldn't be able to touch, and one for actually cleaning up,
  159.  * when the actual server process wants to get rid of the thing,
  160.  * whatever it is.  
  161.  *
  162.  * kill_cleanup disarms a cleanup, presumably because the resource in
  163.  * question has been closed, freed, or whatever, and it's scarce
  164.  * enough to want to reclaim (e.g., descriptors).  It arranges for the
  165.  * resource not to be cleaned up a second time (it might have been
  166.  * reallocated).  run_cleanup does the same, but runs it first.
  167.  *
  168.  * Cleanups are identified for purposes of finding & running them off by the
  169.  * plain_cleanup and data, which should presumably be unique.
  170.  *
  171.  * NB any code which invokes register_cleanup or kill_cleanup directly
  172.  * is a critical section which should be guarded by block_alarms() and
  173.  * unblock_alarms() below...
  174.  */
  175.  
  176. void register_cleanup (pool *p, void *data,
  177.                void (*plain_cleanup)(void *),
  178.                void (*child_cleanup)(void *));
  179.  
  180. void kill_cleanup (pool *p, void *data, void (*plain_cleanup)(void *));
  181.  
  182. /* The time between when a resource is actually allocated, and when it
  183.  * its cleanup is registered is a critical section, during which the
  184.  * resource could leak if we got interrupted or timed out.  So, anything
  185.  * which registers cleanups should bracket resource allocation and the
  186.  * cleanup registry with these.  (This is done internally by run_cleanup).
  187.  *
  188.  * NB they are actually implemented in http_main.c, since they are bound
  189.  * up with timeout handling in general...
  190.  */
  191.  
  192. extern void block_alarms();
  193. extern void unblock_alarms();
  194.  
  195. /* Common cases which want utility support..
  196.  * the note_cleanups_for_foo routines are for 
  197.  */
  198.  
  199. FILE *pfopen(struct pool *, char *name, char *fmode);
  200. FILE *pfdopen(struct pool *, int fd, char *fmode);
  201. int popenf(struct pool *, char *name, int flg, int mode); 
  202.  
  203. void note_cleanups_for_file (pool *, FILE *);
  204. void note_cleanups_for_fd (pool *, int);
  205.  
  206. /* routines to note closes... file descriptors are constrained enough
  207.  * on some systems that we want to support this.
  208.  */
  209.  
  210. int pfclose(struct pool *, FILE *);
  211. int pclosef(struct pool *, int fd);
  212.  
  213. /* ... even child processes (which we may want to wait for,
  214.  * or to kill outright, on unexpected termination).
  215.  *
  216.  * spawn_child is a utility routine which handles an awful lot of
  217.  * the rigamarole associated with spawning a child --- it arranges
  218.  * for pipes to the child's stdin and stdout, if desired (if not,
  219.  * set the associated args to NULL).  It takes as args a function
  220.  * to call in the child, and an argument to be passed to the function.
  221.  */
  222.      
  223. enum kill_conditions { kill_never, kill_always, kill_after_timeout, just_wait};
  224.  
  225. int spawn_child (pool *, void (*)(void *), void *,
  226.          enum kill_conditions, FILE **pipe_in, FILE **pipe_out);
  227.  
  228. /* magic numbers --- only one so far, min free bytes in a new pool block */
  229.  
  230. #define BLOCK_MINFREE 8192     
  231.  
  232. /* Finally, some accounting */
  233.  
  234. long bytes_in_pool(pool *p);
  235. long bytes_in_free_blocks();
  236.